<template>
  <el-dialog width="500" v-model="dialogVisible" :title="isEdit ? '编辑专利' : '添加专利'">
    <el-form :model="form" label-width="auto">
      <el-form-item label="企业名称" prop="name">
        <el-select
          :disabled="isEdit"
          style="width: 200px"
          v-model="options.value"
          placeholder="请选择企业"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="专利名称">
        <el-input placeholder="请输入" v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="申请人">
        <el-input placeholder="请输入" v-model="form.applicant"></el-input>
      </el-form-item>
      <el-form-item label="专利类型">
        <el-select v-model="form.patentType" placeholder="请选择专利类型">
          <el-option label="发明专利" value="发明专利"></el-option>
          <el-option label="实用新型专利" value="实用新型专利"></el-option>
          <el-option label="外观专利" value="外观专利"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间" prop="applicationDate">
        <el-date-picker
          v-model="form.applicationDate"
          type="date"
          :locale="zhCn"
          placeholder="请选择申请时间"
          style="width: 80%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="注册时间" prop="registrationDate">
        <el-date-picker
          :locale="zhCn"
          v-model="form.registrationDate"
          type="date"
          placeholder="请选择注册时间"
          style="width: 80%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="专利摘要">
        <el-input type="textarea" :rows="4" placeholder="请输入" v-model="form.abstract"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="onSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { getEnterpriseAll, addPatent, updatePatent } from '@/api/enterprise'
import { ElMessage } from 'element-plus'
const form = ref({
  name: '',
  applicant: '',
  applicationDate: '',
  registrationDate: '',
  patentType: '',
  abstract: '',
})
const isEdit = ref(false)
const options = ref([])
const dialogVisible = ref(false)
const emit = defineEmits('submit')

const getEnterpriseData = async () => {
  const res = await getEnterpriseAll()
  if (res.status === 201 || res.status === 200) {
    // 处理数据，获取每个企业的信息
    options.value = res.data.data.map((enterprise) => {
      return {
        label: enterprise.name,
        value: enterprise.id,
      }
    })
  }
}
const openDialog = (data = null) => {
  if (data) {
    console.log('111', data)
    isEdit.value = true
    form.value = JSON.parse(JSON.stringify(data))
    options.value.value = data.enterprise.id
  } else {
    isEdit.value = false
    form.value = {
      name: '',
      applicant: '',
      applicationDate: '',
      registrationDate: '',
      patentType: '',
      abstract: '',
    }
    options.value.value = ''
  }
  dialogVisible.value = true
}

// 关闭弹窗方法
const closeDialog = () => {
  dialogVisible.value = false
}
const onSubmit = async () => {
  let res
  if (isEdit.value) {
    res = await updatePatent(form.value)
  } else {
    res = await addPatent({ ...form.value, enterpriseId: options.value.value })
  }

  if (res.status === 200 || res.status === 201) {
    ElMessage.success(isEdit.value ? '编辑成功' : '添加成功')
    emit('submit')
    closeDialog()
  } else {
    ElMessage.error(res.data.data || '系统繁忙，请稍后再试')
  }
}
onMounted(() => {
  getEnterpriseData()
})
defineExpose({ openDialog, closeDialog })
</script>

<style>
</style>
